<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />

<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="author" content="Kevin Wright" />

<meta name="date" content="2018-05-24" />

<title>Bivariate choropleth maps</title>



<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>



<link href="data:text/css;charset=utf-8,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23header%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0Aa%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%20code%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%0A" rel="stylesheet" type="text/css" />

</head>

<body>




<h1 class="title toc-ignore">Bivariate choropleth maps</h1>
<h4 class="author"><em>Kevin Wright</em></h4>
<h4 class="date"><em>2018-05-24</em></h4>



<style type="text/css">
body, td { font-size: 14pt; color: #000; padding:1em; }
code, pre, #ws, #message {border: 0px; font-size: 12pt;
  color: #000; background-color: #f5f5f5; }
code {border: 0px solid #e0e0e0; padding: 0 5px;}
pre > code { border: 0; }
</style>
<div id="r-setup" class="section level1">
<h1>R setup</h1>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">&quot;knitr&quot;</span>)
knitr<span class="op">::</span>opts_chunk<span class="op">$</span><span class="kw">set</span>(<span class="dt">fig.align=</span><span class="st">&quot;center&quot;</span>, <span class="dt">fig.width=</span><span class="dv">6</span>, <span class="dt">fig.height=</span><span class="dv">6</span>)
<span class="kw">options</span>(<span class="dt">width=</span><span class="dv">90</span>)</code></pre></div>
</div>
<div id="single-variate-choropleth-maps" class="section level1">
<h1>Single-variate choropleth maps</h1>
<p>The two maps below show male and female cancer rates on separate maps. The rates are coarsely binned into three classes so as to be somewhat comparable to the bivariate choropleth below.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">require</span>(latticeExtra) <span class="co"># USCancerRates, mapplot</span>
<span class="kw">require</span>(maps) <span class="co"># map</span>
<span class="kw">require</span>(classInt) <span class="co"># classIntervals, findCols</span>
<span class="kw">require</span>(grid) <span class="co"># viewport, pushViewport</span>
<span class="kw">require</span>(pals) <span class="co"># brewer.blues, stevens.pinkgreen</span>

<span class="kw">suppressWarnings</span>(<span class="kw">print</span>(
  <span class="kw">mapplot</span>(<span class="kw">rownames</span>(USCancerRates) <span class="op">~</span><span class="st"> </span><span class="kw">log</span>(rate.male) <span class="op">+</span><span class="st"> </span><span class="kw">log</span>(rate.female),
          <span class="dt">data =</span> USCancerRates,
          <span class="dt">colramp =</span> brewer.blues,
          <span class="dt">map =</span> <span class="kw">map</span>(<span class="st">&quot;county&quot;</span>, <span class="dt">plot =</span> <span class="ot">FALSE</span>, <span class="dt">fill =</span> <span class="ot">TRUE</span>,
                    <span class="dt">projection =</span> <span class="st">&quot;tetra&quot;</span>),
          <span class="dt">breaks=</span><span class="kw">classIntervals</span>(<span class="kw">log</span>(USCancerRates<span class="op">$</span>rate.female), <span class="dt">n=</span><span class="dv">3</span>, <span class="dt">style=</span><span class="st">'quantile'</span>)<span class="op">$</span>brks,
          <span class="dt">scales =</span> <span class="kw">list</span>(<span class="dt">draw =</span> <span class="ot">FALSE</span>))
))</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div id="comments-on-the-single-variate-choropleth-maps" class="section level2">
<h2>Comments on the single-variate choropleth maps</h2>
<ol style="list-style-type: decimal">
<li><p>It is fairly easy to choose a color scheme (e.g. BrewerPal sequential colors).</p></li>
<li><p>It is fairly easy to interpret a single-variate map.</p></li>
<li><p>If two single-variate maps are drawn with a common scale for the variates (as shown here), the breakpoints for the colors might be suitable for one variate and less suited for the other variable. For example, the rates for females are high in West Virginia and the lower Mississippi River valley, but the breakpoints on the color scale do not allow for identification of similar features for males.</p></li>
</ol>
</div>
<div id="bivariate-color-classes" class="section level2">
<h2>Bivariate color classes</h2>
<p>Each variate is divided into thirds (based on percentiles) and a joint classification for all 9 combinations is defined.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cols &lt;-<span class="st"> </span>stevens.pinkgreen; nbins &lt;-<span class="st"> </span><span class="dv">3</span>

<span class="co"># categorize rates into 3 percentile bins</span>
brksm &lt;-<span class="st"> </span><span class="kw">classIntervals</span>(<span class="kw">log</span>(USCancerRates<span class="op">$</span>rate.male), <span class="dt">n=</span>nbins, <span class="dt">style=</span><span class="st">'quantile'</span>)</code></pre></div>
<pre><code>## Warning in classIntervals(log(USCancerRates$rate.male), n = nbins, style = &quot;quantile&quot;):
## var has missing values, omitted in finding classes</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">brksf &lt;-<span class="st"> </span><span class="kw">classIntervals</span>(<span class="kw">log</span>(USCancerRates<span class="op">$</span>rate.female), <span class="dt">n=</span>nbins, <span class="dt">style=</span><span class="st">'quantile'</span>)</code></pre></div>
<pre><code>## Warning in classIntervals(log(USCancerRates$rate.female), n = nbins, style = &quot;quantile&quot;):
## var has missing values, omitted in finding classes</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">classm &lt;-<span class="st"> </span><span class="kw">findCols</span>(brksm)
classf &lt;-<span class="st"> </span><span class="kw">findCols</span>(brksf)
<span class="co"># convert x,y classes into a joint class x+3(y-1)</span>
USCancerRates<span class="op">$</span>class2 &lt;-<span class="st"> </span>classm <span class="op">+</span><span class="st"> </span>nbins<span class="op">*</span>(classf<span class="op">-</span><span class="dv">1</span>)

<span class="co"># scatterplot of two variates showing bins</span>
<span class="kw">plot</span>(<span class="kw">log</span>(rate.female) <span class="op">~</span><span class="st"> </span><span class="kw">log</span>(rate.male), <span class="dt">data=</span>USCancerRates,
     <span class="dt">col=</span><span class="kw">cols</span>()[USCancerRates<span class="op">$</span>class2], <span class="dt">pch=</span><span class="dv">19</span>,
     <span class="dt">xlim=</span><span class="kw">c</span>(<span class="fl">4.5</span>, <span class="fl">6.5</span>), <span class="dt">ylim=</span><span class="kw">c</span>(<span class="fl">4.0</span>, <span class="dv">6</span>))</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="bivariate-choropleth-maps" class="section level1">
<h1>Bivariate choropleth maps</h1>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">m3 &lt;-<span class="st"> </span><span class="kw">mapplot</span>(<span class="kw">rownames</span>(USCancerRates) <span class="op">~</span><span class="st"> </span>class2, <span class="dt">data =</span> USCancerRates,
              <span class="dt">colramp=</span>cols, <span class="dt">breaks=</span><span class="kw">seq</span>(<span class="dt">from=</span><span class="fl">0.5</span>, <span class="dt">by=</span><span class="dv">1</span>, <span class="dt">length=</span>nbins<span class="op">*</span>nbins<span class="op">+</span><span class="dv">1</span>),
              <span class="dt">xlab=</span><span class="st">&quot;&quot;</span>,
              <span class="dt">colorkey=</span><span class="ot">FALSE</span>,
              <span class="dt">map =</span> <span class="kw">map</span>(<span class="st">&quot;county&quot;</span>, <span class="dt">plot =</span> <span class="ot">FALSE</span>, <span class="dt">fill =</span> <span class="ot">TRUE</span>,
                        <span class="dt">projection =</span> <span class="st">&quot;tetra&quot;</span>),
              <span class="dt">scales =</span> <span class="kw">list</span>(<span class="dt">draw =</span> <span class="ot">FALSE</span>))
<span class="kw">suppressWarnings</span>(<span class="kw">print</span>( m3 ))
<span class="co"># add the color legend</span>
m3leg &lt;-<span class="st"> </span><span class="kw">levelplot</span>(<span class="kw">matrix</span>(<span class="dv">1</span><span class="op">:</span>(nbins<span class="op">*</span>nbins), <span class="dt">nrow=</span>nbins), <span class="dt">axes=</span><span class="ot">FALSE</span>, <span class="dt">col.regions=</span><span class="kw">cols</span>(),
                   <span class="dt">xlab=</span><span class="st">&quot;male  --&gt;&quot;</span>, <span class="dt">ylab=</span><span class="st">&quot;female  --&gt;&quot;</span>, <span class="dt">cuts=</span><span class="dv">8</span>, <span class="dt">colorkey=</span><span class="ot">FALSE</span>,
                   <span class="dt">scales=</span><span class="kw">list</span>(<span class="dt">draw=</span><span class="dv">0</span>))
vp &lt;-<span class="st"> </span><span class="kw">viewport</span>(<span class="dt">x=</span>.<span class="dv">15</span>, <span class="dt">y=</span>.<span class="dv">25</span>, <span class="dt">width=</span>.<span class="dv">2</span>, <span class="dt">height=</span>.<span class="dv">2</span>)
<span class="kw">pushViewport</span>(vp)
<span class="kw">print</span>(m3leg, <span class="dt">newpage=</span><span class="ot">FALSE</span>)
<span class="kw">popViewport</span>()</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># test</span>
<span class="kw">print</span>(m3, <span class="dt">more=</span><span class="ot">TRUE</span>)</code></pre></div>
<pre><code>## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 64 unmatched
## regions: alaska,nome, alaska,wade hampton, alaska,haines, alaska,....</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">print</span>(m3leg, <span class="dt">position=</span><span class="kw">c</span>(.<span class="dv">05</span>, .<span class="dv">15</span>, .<span class="dv">25</span>, .<span class="dv">35</span>), <span class="dt">more=</span><span class="ot">FALSE</span>)</code></pre></div>
<p><img src="" style="display: block; margin: auto;" /></p>
<div id="comments-on-the-bivariate-choropleth-map" class="section level2">
<h2>Comments on the bivariate choropleth map</h2>
<ol style="list-style-type: decimal">
<li><p>Choosing a good color scheme requires some care. See <span class="citation">Stevens (2015)</span>.</p></li>
<li><p>The single map is larger (than two single-variate choropleth maps), which makes it easier to see individual counties.</p></li>
<li><p>The larger map also reduces the relative amount of black ink to draw the county boundaries.</p></li>
<li><p>Because there are few bins for each variate, within-bin structure for individual variates could be overlooked.</p></li>
</ol>
<p>In the example above, the rates of cancer are high for both men and women in West Virginia and the lower Mississippi River valley. South and east of that area, the counties are more green than blue, indicating a higher rate for men than for women.</p>
</div>
</div>
<div id="references" class="section level1 unnumbered">
<h1>References</h1>
<div id="refs" class="references">
<div id="ref-stevens2015bivariate">
<p>Stevens, Josh. 2015. “Bivariate Choropleth Maps: A How-to Guide.” Blog. <em>Josh Stevens</em>. <a href="http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/" class="uri">http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/</a>.</p>
</div>
</div>
</div>



<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
